function data = bciReadRecorderData(bci,hdr,begsample,endsample)
% data = bciReadRecorderData(bci,hdr,varargin) %Read a junk of data from the buffer of a recording system
%
% PURPOSE:
% Read a slice of data from then buffer of a recording system.
%
% INPUT:
% bci:          A bci structure
% hdr:          A hdr structure initialized and returned by bciReadRecorderHeader
% begsample:    The first sample to read from the data buffer
% endsample:    The last sample to read from the data buffer
%
% OUTPUT:
% data:         The data read from the buffer. Channels along the rows,
%               timepoints along the columns.
%
% TODO:
% Christoph: The new calling conetion must be impeented in all calling
% functions
% Christoph: I moved the calibration here where the machine dependetn stuff
% happens. Check the functionality of the bci.init.calib and better move the 
% parameters to the hdr structure since they are machine dependent   

% 1001202 JR wrote it.

%check input
if nargin ~= 4
    error('Four input parameters expected.')
end

if bci.init.recordingSystemID==1, % TDT system %strcmpi(bci.recordingSystem,'tdt'),
    data=bciReadDataTDT(bci,hdr,begsample,endsample);
elseif bci.init.recordingSystemID==2, % BTINIclient communication
    data = bci.init.calib * single(BTINImex(begsample, endsample));
else     % assume TCP/IP buffer
    data = bci.init.calib * single(bci.param.ftReadFunc(bci.dataBufferName, 'header', hdr, 'begsample', begsample, 'endsample', endsample));
    % read_data is a fieldtrip function
end
